﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

namespace tryvideo
{
    public partial class MainWindow : Window
    {
        [DllImport("writevideo.dll", EntryPoint = "fnInitVideo", CallingConvention = CallingConvention.Cdecl)]
        static extern int fnInitVideo(string name, int fcc, double fps, int w, int h);
        [DllImport("writevideo.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int fnWriteVideo(byte[] data, int bytePerPixel);
        [DllImport("writevideo.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern void fnTest(byte[] name);


        public MainWindow()
        {
            Bitmap bm = new Bitmap("gray.jpg");
            Console.WriteLine(bm.GetPixel(0, 0).ToString());
            Console.WriteLine(bm.GetPixel(0, 1).ToString());
            Console.WriteLine(bm.GetPixel(0, 2).ToString());
            Console.WriteLine(bm.GetPixel(1, 0).ToString());
            Console.WriteLine(bm.GetPixel(2, 0).ToString());
            InitializeComponent();

            Console.WriteLine(fnInitVideo("hello.avi", -1, 30, 640, 480));
//             
//             BitmapSource bs;
//             bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero,
//                 Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
//             BitmapData data = bm.LockBits(new System.Drawing.Rectangle(0, 0, bm.Width, bm.Height),
//                 ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//             unsafe
//             {
//                 char* ptr = (char*)(data.Scan0);
//                 string s = new string(ptr);
//             }
// 
//             Console.WriteLine("w {0} h {1}, pw {2} ph {3}", bs.Width, bs.Height, bs.PixelWidth, bs.PixelHeight);
//             Console.WriteLine("bits per pixel {0}", bs.Format.BitsPerPixel);
//             int stride = bs.PixelWidth * bs.Format.BitsPerPixel / 8;
//             Console.WriteLine("stride {0}", stride);
//             byte[] dataArr = new byte[bs.PixelHeight*stride];
//             bs.CopyPixels(dataArr, stride, 0);
//             for (int i = 0; i < 10; ++i)
//                 Console.Write("{0} ", dataArr[i]);
//             Console.WriteLine();
//             fnTest(dataArr);
        }
    }
}
